clear all
set more off 

cd "C:\Dropbox\Average_target_survey\replication_folder\dofiles"
quiet do load_rreg2

cd "C:\Dropbox\Average_target_survey\replication_folder\workfiles"
use step001, clear

***collapse days when the sample size is small
gen start_day2=start_day1
recode start_day2 30/32=29

gen prob_prior=	epi_12m_12plus + epi_12m_8_12 + epi_12m_4_8

tempname 1
postfile `1'  str50 variable ///
	str50 E_method ///
	str12 b_heard_news ///
	str12 b_AFTER ///
	str12 b_heard_news_X_AFTER ///
	str12 R2 ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table03_news_OLS.dta", replace every(1)
	
tempname 2
postfile `2'  str50 variable ///
	str50 E_method ///
	str12 b_heard_news ///
	str12 b_AFTER ///
	str12 b_heard_news_X_AFTER ///
	str12 R2 ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table03_news_OLS_restricted.dta", replace every(1)
		
tempname 3
postfile `3'  str50 variable ///
	str50 E_method ///
	str12 b_heard_news ///
	str12 b_AFTER ///
	str12 b_heard_news_X_AFTER ///
	str12 R2 ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table03_news_OLS_unreasonable_share.dta", replace every(1)

tempname 4
postfile `4'  str50 variable ///
	str50 E_method ///
	str12 b_heard_news ///
	str12 b_AFTER ///
	str12 b_heard_news_X_AFTER ///
	str12 R2 ///
	using "C:\Dropbox\Average_target_survey\replication_folder\outregs\Table03_news_Huber.dta", replace every(1)
		
		
* hearing news about the Fed	
gen heard_news=(QJH3==2)
gen _heard_news=(QJH3==2)*sqrt(weight)

*** condition on picking IT as the policy regime
gen IT_pick=(QJH10_2==1 | QJH10_1==1)

*** pick the right Fed target: maximum employment AND stable price
gen target=(QJH9_2==1 & QJH9_5==1)

*** post-announcement sample 
gen AFTER=(start_day2>26)  
gen _AFTER=AFTER*sqrt(weight)

*** interaction of heard_news and AFTER
gen heard_news_X_AFTER=heard_news*AFTER
gen _heard_news_X_AFTER=heard_news_X_AFTER*sqrt(weight)

*** create dummy variables for categorical variables
* education
forvalues i=2(1)6 {
	gen Q48_`i'=(Q48==`i')
	gen _Q48_`i'=(Q48==`i')*sqrt(weight)
	
}

* income
forvalues i=1(1)6 {
	gen Q49_`i'=(Q49==`i')
	gen _Q49_`i'=(Q49==`i')*sqrt(weight)
	
}

* how people get news
forvalues i=1(1)9 {
	gen _QJH1_`i'=QJH1_`i'*sqrt(weight)
}

* how often people get news
forvalues i=2(1)8 {
	gen QJH2_`i'=(QJH2==`i')
	gen _QJH2_`i'=QJH2_`i'*sqrt(weight)
}

foreach var in age male N_children married single nonwhite hispanic party_democrat party_republican party_other {
	gen _`var'=`var'*sqrt(weight)
}
local CONTROLS = " age male N_children married single nonwhite hispanic Q48_*  Q49_* QJH1_? QJH2_?  party_democrat party_republican party_other "
local _CONTROLS = " _age _male _N_children _married _single _nonwhite _hispanic _Q48_*  _Q49_*   _party_democrat _party_republican _party_other "

gen _W=sqrt(weight)
	
gen credibility = QJH17_1 if treatment_IT==0 & treatment_AIT==0
	
foreach var in 	target /// pick the right Fed target: maximum employment AND stable price
				IT_pick /// picking IT as the policy regime
				QJH11 /// Inflation target of the Fed
				epi_12m ///
				epi_12m_mean ///
				prob_prior ///
				epi_12m_std ///
				egdp_12m /// 
				edy_12m ///
				credibility /// credbility of the Fed (use only the control group)
				Q164_7 /// consumption plan Sept				
				{
		
		    
					
			capture drop Y_flag 
			gen Y_flag=(abs(`var')>20)
			
			capture drop _`var'
			gen _`var'=`var'*sqrt(weight)
			
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** raw mean
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			reg `var' heard_news AFTER heard_news_X_AFTER `CONTROLS' [aw=weight], robust
		
			local b_heard_news=string(_b[heard_news],"%4.3f")
			local b_AFTER=string(_b[AFTER],"%4.3f")
			local b_heard_news_X_AFTER=string(_b[heard_news_X_AFTER],"%4.3f")

			local se_heard_news="(" + string(_se[heard_news],"%4.3f") + ")"
			local se_AFTER="(" + string(_se[AFTER],"%4.3f") + ")"
			local se_heard_news_X_AFTER="(" + string(_se[heard_news_X_AFTER],"%4.3f") + ")"
			
			local p1=_b[heard_news]/_se[heard_news]
			local p2=_b[AFTER]/_se[AFTER]
			local p3=_b[heard_news_X_AFTER]/_se[heard_news_X_AFTER]
			di `p3'
			
			if abs(`p1')>2.62 & `p1'~=. local b_heard_news="`b_heard_news'" + "*"
			if abs(`p2')>2.62 & `p2'~=. local b_AFTER="`b_AFTER'" + "*"
			if abs(`p3')>2.62 & `p3'~=. local b_heard_news_X_AFTER="`b_heard_news_X_AFTER'" + "*"	
			
			if abs(`p1')>1.98 & `p1'~=. local b_heard_news="`b_heard_news'" + "*"
			if abs(`p2')>1.98 & `p2'~=. local b_AFTER="`b_AFTER'" + "*"
			if abs(`p3')>1.98 & `p3'~=. local b_heard_news_X_AFTER="`b_heard_news_X_AFTER'" + "*"
			
			if abs(`p1')>1.65 & `p1'~=. local b_heard_news="`b_heard_news'" + "*"
			if abs(`p2')>1.65 & `p2'~=. local b_AFTER="`b_AFTER'" + "*"
			if abs(`p3')>1.65 & `p3'~=. local b_heard_news_X_AFTER="`b_heard_news_X_AFTER'" + "*"
		
			local R2=string(e(r2),"%4.3f")
 
			
			post `1' ("`var'") ("OLS")	("`b_heard_news'") ("`b_AFTER'") ("`b_heard_news_X_AFTER'")  ("`R2'")
			post `1' ("`var'") ("OLS")	("`se_heard_news'") ("`se_AFTER'") ("`se_heard_news_X_AFTER'")  ("")
			
			
			
			
			if "`var'"~="target" &  "`var'"~="IT_pick" & "`var'"~="Q164_1"   & "`var'"~="Q164_6"   & "`var'"~="Q164_7" {
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** restricted mean (exclude people outside [-20,20])
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			reg `var' heard_news AFTER heard_news_X_AFTER `CONTROLS' [aw=weight]  if Y_flag==0, robust
		
			local rb_heard_news=string(_b[heard_news],"%4.3f")
			local rb_AFTER=string(_b[AFTER],"%4.3f")
			local rb_heard_news_X_AFTER=string(_b[heard_news_X_AFTER],"%4.3f")

			local rse_heard_news="(" + string(_se[heard_news],"%4.3f") + ")"
			local rse_AFTER="(" + string(_se[AFTER],"%4.3f") + ")"
			local rse_heard_news_X_AFTER="(" + string(_se[heard_news_X_AFTER],"%4.3f") + ")"
			
			local p1=_b[heard_news]/_se[heard_news]
			local p2=_b[AFTER]/_se[AFTER]
			local p3=_b[heard_news_X_AFTER]/_se[heard_news_X_AFTER]
			
			
			if abs(`p1')>2.62 & `p1'~=. local rb_heard_news="`rb_heard_news'" + "*"
			if abs(`p2')>2.62 & `p2'~=. local rb_AFTER="`rb_AFTER'" + "*"
			if abs(`p3')>2.62 & `p3'~=. local rb_heard_news_X_AFTER="`rb_heard_news_X_AFTER'" + "*"	
			
			
			if abs(`p1')>1.98 & `p1'~=. local rb_heard_news="`rb_heard_news'" + "*"
			if abs(`p2')>1.98 & `p2'~=. local rb_AFTER="`rb_AFTER'" + "*"
			if abs(`p3')>1.98 & `p3'~=. local rb_heard_news_X_AFTER="`rb_heard_news_X_AFTER'" + "*"
			
			if abs(`p1')>1.65 & `p1'~=. local rb_heard_news="`rb_heard_news'" + "*"
			if abs(`p2')>1.65 & `p2'~=. local rb_AFTER="`rb_AFTER'" + "*"
			if abs(`p3')>1.65 & `p3'~=. local rb_heard_news_X_AFTER="`rb_heard_news_X_AFTER'" + "*"

			
			local rR2=string(e(r2),"%4.3f")
			
			post `2' ("`var'") ("restricted OLS")	("`rb_heard_news'") ("`rb_AFTER'") ("`rb_heard_news_X_AFTER'")  ("`rR2'")
			post `2' ("`var'") ("restricted OLS")	("`rse_heard_news'") ("`rse_AFTER'") ("`rse_heard_news_X_AFTER'")  ("")
			
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** share of people with unreasonable expectations (outside [-20,20])
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			reg Y_flag heard_news AFTER heard_news_X_AFTER `CONTROLS' [aw=weight], robust
		
			local sb_heard_news=string(_b[heard_news],"%4.3f")
			local sb_AFTER=string(_b[AFTER],"%4.3f")
			local sb_heard_news_X_AFTER=string(_b[heard_news_X_AFTER],"%4.3f")

			local sse_heard_news="(" + string(_se[heard_news],"%4.3f") + ")"
			local sse_AFTER="(" + string(_se[AFTER],"%4.3f") + ")"
			local sse_heard_news_X_AFTER="(" + string(_se[heard_news_X_AFTER],"%4.3f") + ")"
			
			local p1=_b[heard_news]/_se[heard_news]
			local p2=_b[AFTER]/_se[AFTER]
			local p3=_b[heard_news_X_AFTER]/_se[heard_news_X_AFTER]
			
			if abs(`p1')>2.62 & `p1'~=. local sb_heard_news="`sb_heard_news'" + "*"
			if abs(`p2')>2.62 & `p2'~=. local sb_AFTER="`sb_AFTER'" + "*"
			if abs(`p3')>2.62 & `p3'~=. local sb_heard_news_X_AFTER="`sb_heard_news_X_AFTER'" + "*"	
			
			if abs(`p1')>1.98 & `p1'~=. local sb_heard_news="`sb_heard_news'" + "*"
			if abs(`p2')>1.98 & `p2'~=. local sb_AFTER="`sb_AFTER'" + "*"
			if abs(`p3')>1.98 & `p3'~=. local sb_heard_news_X_AFTER="`sb_heard_news_X_AFTER'" + "*"
			
			if abs(`p1')>1.65 & `p1'~=. local sb_heard_news="`sb_heard_news'" + "*"
			if abs(`p2')>1.65 & `p2'~=. local sb_AFTER="`sb_AFTER'" + "*"
			if abs(`p3')>1.65 & `p3'~=. local sb_heard_news_X_AFTER="`sb_heard_news_X_AFTER'" + "*"			
			
			local sR2=string(e(r2),"%4.3f")
			
			post `3' ("`var'") ("share unreasonable responses")	("`sb_heard_news'") ("`sb_AFTER'") ("`sb_heard_news_X_AFTER'")  ("`sR2'")
			post `3' ("`var'") ("share unreasonable responses")	("`sse_heard_news'") ("`sse_AFTER'") ("`sse_heard_news_X_AFTER'")  ("")
			
			}
			
			
			* if "`var'"~="target" &  "`var'"~="IT_pick" {

			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			*** Huber-robust mean 
			***~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			capture drop wgt_Y
			* rreg2 _`var' _aug26 _aug27 _aug28 _aug29, nolog gen(wgt_Y) tune(3)
			capture {
			rreg2 _`var' _heard_news _AFTER _heard_news_X_AFTER `_CONTROLS' _W , nolog gen(wgt_Y)
			replace wgt_Y=wgt_Y*weight
			
			 reg `var' heard_news AFTER heard_news_X_AFTER `CONTROLS' [aw=wgt_Y], robust
			}
			
			*** OLS regression for dummy variable outcomes
			if "`var'"=="target" {
				reg `var' heard_news AFTER heard_news_X_AFTER `CONTROLS' [aw=weight], robust
			}
			
			*** OLS regression for dummy variable outcomes
			if "`var'"=="IT_pick" {
				reg `var' heard_news AFTER heard_news_X_AFTER `CONTROLS' [aw=weight], robust
			}
			
			local hb_heard_news=string(_b[heard_news],"%4.3f")
			local hb_AFTER=string(_b[AFTER],"%4.3f")
			local hb_heard_news_X_AFTER=string(_b[heard_news_X_AFTER],"%4.3f")

			local hse_heard_news="(" + string(_se[heard_news],"%4.3f") + ")"
			local hse_AFTER="(" + string(_se[AFTER],"%4.3f") + ")"
			local hse_heard_news_X_AFTER="(" + string(_se[heard_news_X_AFTER],"%4.3f") + ")"
			
			local p1=_b[heard_news]/_se[heard_news]
			local p2=_b[AFTER]/_se[AFTER]
			local p3=_b[heard_news_X_AFTER]/_se[heard_news_X_AFTER]
		
			if abs(`p1')>2.62 & `p1'~=. local hb_heard_news="`hb_heard_news'" + "*"
			if abs(`p2')>2.62 & `p2'~=. local hb_AFTER="`hb_AFTER'" + "*"
			if abs(`p3')>2.62 & `p3'~=. local hb_heard_news_X_AFTER="`hb_heard_news_X_AFTER'" + "*"		
			
			if abs(`p1')>1.98 & `p1'~=. local hb_heard_news="`hb_heard_news'" + "*"
			if abs(`p2')>1.98 & `p2'~=. local hb_AFTER="`hb_AFTER'" + "*"
			if abs(`p3')>1.98 & `p3'~=. local hb_heard_news_X_AFTER="`hb_heard_news_X_AFTER'" + "*"
			
			if abs(`p1')>1.65 & `p1'~=. local hb_heard_news="`hb_heard_news'" + "*"
			if abs(`p2')>1.65 & `p2'~=. local hb_AFTER="`hb_AFTER'" + "*"
			if abs(`p3')>1.65 & `p3'~=. local hb_heard_news_X_AFTER="`hb_heard_news_X_AFTER'" + "*"
			
			local hR2=string(e(r2),"%4.3f")
			
			post `4' ("`var'") ("Huber")	("`hb_heard_news'") ("`hb_AFTER'") ("`hb_heard_news_X_AFTER'")  ("`hR2'")
			post `4' ("`var'") ("Huber")	("`hse_heard_news'") ("`hse_AFTER'") ("`hse_heard_news_X_AFTER'")  ("")
			
			* }
			
}

postclose `1'
postclose `2'
postclose `3'
postclose `4'

tab start_day2
